package com.woniu.woniuproject.web;

import com.woniu.woniuproject.entity.Account;
import com.woniu.woniuproject.entity.Undertaker;
import com.woniu.woniuproject.entity.User;
import com.woniu.woniuproject.mapper.AccountMapper;
import com.woniu.woniuproject.mapper.UndertakerMapper;
import com.woniu.woniuproject.mapper.moneyCountMapper;
import com.woniu.woniuproject.utils.AlipayUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("invest")
public class InvestController {
    @Autowired
    AccountMapper accountMapper;
    @Autowired
    moneyCountMapper moneycountMapper;

@RequestMapping("/list")
    public Map<String,Account> list(){
    HashMap<String,Account> map=new HashMap<>();
    User id = (User) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
    Account account = accountMapper.selectByPrimaryKey(id.getId());
    map.put("data",account);
    return map;

}

@RequestMapping("/zhifubao")
public void zhifubao(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //使用当前系统时间作为订单号
    String tradeNo=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());

    //获取付款金额
    String money = req.getParameter("money");
    User user = (User)SecurityUtils.getSubject().getSession().getAttribute("loginUser");
    Integer borrowerid = user.getId();
    Double macthedmoney=Double.parseDouble(money);
    accountMapper.updateByUserKey(borrowerid,macthedmoney);
    AlipayUtil.getAlipayClient(tradeNo, money,  req, resp);

}

    @Autowired
    UndertakerMapper undertakerMapper;
    @RequestMapping("/tixian")
    public String tixian(@RequestParam("money") Double money){
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
        Integer id = user.getId();
        Undertaker undertaker=new Undertaker();
        undertaker.setId(id);
        undertaker.setUndertaker(String.valueOf(money));
        Double cost=money*0.01;
        undertaker.setCost(cost);
        String data="0";
        int i = undertakerMapper.insert(undertaker);
        if(i>0){
            data="1";
        }
        return data;
    }
}
